package com.jadefashion.mobile;

import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.pojo.Member;
import com.jadefashion.service.MemberService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;

/**
 * @author:XWS
 * @Date:2020/8/4
 * @Description:com.itheima.controller
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    RedisTemplate redisTemplate;
    @Reference
    private MemberService memberService;
    @RequestMapping("/addMemberForLogin.do")
    public Result addOrderForLogin(@RequestParam("telephone")String telephone, @RequestParam("validateCode") String validateCode){

        try{
//            Jedis jedis = jedisPool.getResource();
//            String redisCode = jedis.get(telephone);
            String redisCode = (String) redisTemplate.opsForValue().get(telephone);
            if(redisCode==null||redisCode.length()==0){
                return new Result(false,"验证码过期,请重新发送");
            }else {
                if (redisCode.equals(validateCode)){
                    //判断是否为会员
                    //通过手机号查询是否为会员
                    Member member=memberService.findMemberByTelephone(telephone);
                    if (member==null){
                        //新会员，做添加操作
                        member = new Member();
                        member.setPhoneNumber(telephone);
                        member.setRegTime(new Date());
                        memberService.add(member);
                    }

                    return new Result(true, MessageConstant.ADD_ORDER_SUCCESS);
                }else {
                    return new Result(false, "验证码不匹配");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }
}
